package com.horoscope.astrology.zodiac.signs.free.adapter

import android.app.Activity
import android.content.Context
import android.os.Build
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat.getColor
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.NativeADViewHolder
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.ShareViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.ArticleBean
import com.horoscope.astrology.zodiac.signs.free.bean.NativeAdBean
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager

class ArticleDetailAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mDataList = ArrayList<ArticleBean>()
    private var loadFinished: Boolean = false
    private var mTitle = ""

    fun setData(list: ArrayList<ArticleBean>) {
        mDataList.addAll(0, list)
        mDataList.add(ArticleBean())//share
        notifyDataSetChanged()

    }

    fun setTitle(title: String) {
        mTitle = title
        mDataList.add(0, ArticleBean())//站位
        notifyDataSetChanged()
    }


    fun setLoadFinished(loadFinished: Boolean) {
        this.loadFinished = loadFinished
        notifyDataSetChanged()
        notifyItemChanged(itemCount)
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        when (p1) {
            SHARE -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_share, parent, false)
                return ShareViewHolder(view)
            }
            AD -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.ad_layout, parent, false)
                return NativeADViewHolder(mContext, view)
            }
            TITLE -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_article_detail_title, parent, false)
                return TitleViewHolder(view)
            }
            PARAGRAPH -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_article_detail_content, parent, false)
                return ContentHolder(view)
            }
            IMAGE -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_article_detail_pic, parent, false)
                return ImageHolderWithOnePic(view)
            }
            else -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_article_detail_title, parent, false)
                return TitleViewHolder(view)
            }
        }

    }

    override fun getItemCount(): Int {
        return mDataList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, p1: Int) {

        val bean = mDataList[p1]
        when (holder) {
            is TitleViewHolder -> {
                holder.title.text = mTitle
            }
            is ImageHolderWithOnePic -> {
                ImageLoader.loadImage(holder.pic, bean.url)
            }
            is ContentHolder -> {
                if (bean.content.contains("<h2>")) {
                    val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, -2)
                    val num = SysUtils.rp(15f)
                    lp.setMargins(0, num, 0, num)
                    holder.paragraph.layoutParams = lp
                } else {

                    val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, -2)
                    val num = SysUtils.rp(7f)
                    lp.setMargins(0, num, 0, num)
                    holder.paragraph.layoutParams = lp
                }
                var content = bean.content.replace("<h2>", "")
                content = content.replace("<h1>", "")
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    holder.paragraph.text = Html.fromHtml(content, 1)
                } else {
                    holder.paragraph.text = Html.fromHtml(content)
                }
            }
            is NativeADViewHolder -> {
                val mBean = mDataList[p1]

                if (mBean.nativeAdBean == null) {
                    val nativeAdBean = ADNativeManager.mInstance.getNativeAdBean("Native_Content")
                    mDataList[p1].nativeAdBean = nativeAdBean
                    handleNativeAd(nativeAdBean, holder, "Native_Content")
                } else {
                    handleNativeAd(mBean.nativeAdBean, holder, "")
                }

            }
            is ShareViewHolder -> {
                holder.share.setTextColor(getColor(mContext, R.color.gray_7))
                holder.facebook.setOnClickListener {
                    ShareManager.shareToFacebook(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.more.setOnClickListener {
                    ShareManager.sysCallsShare(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.lineLeft.setBackgroundColor(getColor(mContext, R.color.def_gray))
                holder.lineRight.setBackgroundColor(getColor(mContext, R.color.def_gray))
            }
        }
    }

    override fun getItemViewType(position: Int): Int {

        return when {
            position == 0 -> {
                TITLE
            }
            position == mDataList.size - 1 -> {
                SHARE
            }
            mDataList[position].ad -> {
                AD
            }
            else -> {
                when (mDataList[position].type) {
                    "paragraph" -> {
                        PARAGRAPH
                    }
                    "image" -> {
                        IMAGE
                    }
                    else -> {
                        1
                    }
                }
            }
        }
    }

    companion object {
        private const val TITLE = 0x12
        private const val PARAGRAPH = 0x13
        private const val IMAGE = 0x14
        private const val AD = 0x15
        private const val SHARE = 0x16
    }


    private fun handleNativeAd(
        nativeAdBean: NativeAdBean?,
        holder: NativeADViewHolder,
        placement: String
    ) {
        if (nativeAdBean != null) {
            holder.changeAD(nativeAdBean.adInfo.advertiser)
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    if (nativeAdBean.facebookNativeAD != null) {
                        holder.inflateFacebookAd(
                            nativeAdBean.facebookNativeAD,
                            "Custom_Medium",
                            placement
                        )
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
                ADConfig.ADMOB -> {
                    if (nativeAdBean.googleNativeAd != null) {
                        holder.inflateAdMobAd(nativeAdBean.googleNativeAd!!, "Custom_Medium")
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
            }
        } else {//没有拿到广告，从缓存中取
            holder.inVisibility()
        }
    }


    open inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class TitleViewHolder(itemView: View) : MyViewHolder(itemView) {
        val title = itemView.findViewById<TextView>(R.id.text_view_article_detail_title)!!
    }

    inner class ImageHolderWithOnePic(itemView: View) : MyViewHolder(itemView) {
        val pic = itemView.findViewById<ImageView>(R.id.image_view_pic)!!
    }

    inner class ContentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val paragraph: TextView = itemView.findViewById(R.id.text_view_article_detail_content)
    }
}